/***********************************************************************
 * Copyright @ 2011 Zhejiang Yutian Technology Co.,Ltd
 * ==================================================================
 * Project:        LaYouNet
 * FileName:       BaseColumnFragment.java
 * Describtion:    文件描述
 * Author:         xufei
 * Create Date:    2014-7-24 下午4:11:08
 *********************************************************************/
package net.hzlj.platform.enterprise.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import net.hzlj.platform.enterprise.common.EventBusParameter;
import net.hzlj.platform.enterprise.common.WeakReferenceHandler;
import net.hzlj.platform.enterprise.task.Task;
import net.hzlj.platform.enterprise.task.TaskListener;
import net.hzlj.platform.enterprise.widght.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.finalteam.okhttpfinal.HttpCycleContext;
import cn.finalteam.okhttpfinal.HttpTaskHandler;

/**
 * @ClassName: BaseFragment
 * @Description: BaseFragment 基类Fragment
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-27 15:39:51
 */
public class BaseFragment extends Fragment implements HttpCycleContext ,TaskListener {

    protected final String HTTP_TASK_KEY = "http_request_task_fragment_key_" + hashCode();

    public boolean mHasNextPage = false;//是否有分页

    public boolean mIsLoading = true;//当前数据是否在加载

    public boolean mIsStart = true;//是否是第一次调用接口

    public int mCurrentIndex = 0;//当前页码

    Object tag, arg1, arg2;

    public void setTag(Object tag) {
        this.tag = tag;
    }

    public Object getFragmentTag() {
        return tag;
    }

    public void setArg1(Object arg1) {
        this.arg1 = arg1;
    }

    public void setArg2(Object arg2) {
        this.arg2 = arg2;
    }

    private int mNetInitState = NetState.INIT_FIRST;

    public LoadingDialog mLoadingDialog;

    public static class NetState {
        public final static int INIT_FIRST = 0;
        public final static int INITING = 1;
        public final static int INIT_SUCCESS = 2;
        public final static int INIT_FAILED = 3;
        public final static int INIT_NO_NETWORK = 4;
    }

    public static class DataState {
        public final static int DATA_FIRST = 0;
        public final static int DATA_LOADING = 1;
        public final static int DATA_REFRESH = 2;
        public final static int DATA_INIT_COMPLETE = 3;
        public final static int DATA_SEARCH = 4;
        public final static int DATA_MORE = 5;
    }

    public int getNetInitState() {
        return mNetInitState;
    }

    public void setNetInitState(int mNetInitState) {
        this.mNetInitState = mNetInitState;
    }


    public void onFrameResume() {
    }

    public void onNetChanged() {
    }

    public void onFrameStop() {
    }


    public void initData(int netState) {

    }

    void handleMessage(BaseFragment fragment, Message msg) {

    }

    public void onDataCallback(Task task) {

    }

    @Override
    public void onTaskResult(Task task) {
        onDataCallback(task);
    }

    static class MyHandler<T> extends WeakReferenceHandler<T> {

        public MyHandler(T reference) {
            super(reference);
        }

        @Override
        protected void handleMessage(T reference, Message msg) {
            BaseFragment fragment = (BaseFragment) reference;
            fragment.handleMessage(fragment, msg);
        }

    }

    public void onClick(View view) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBackground(EventBusParameter parameter) {
        onEventRunBackground(parameter);
    }

    protected void onEventRunBackground(EventBusParameter parameter) {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        mLoadingDialog= new LoadingDialog(getActivity());
    }

    @Override
    public String getHttpTaskKey() {
        return HTTP_TASK_KEY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLoadingDialog != null){
            mLoadingDialog.dismiss();
        }
        EventBus.getDefault().unregister(this);//反注册EventBus
        HttpTaskHandler.getInstance().removeTask(HTTP_TASK_KEY);
    }
}
